package com.tttn.docbao.fragment;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.tttn.docbao.R;
import com.tttn.docbao.activity.NewsActivity;
import com.tttn.docbao.activity.ReadActivity;
import com.tttn.docbao.adapter.TitleAdapter;
import com.tttn.docbao.db.JSONParser;
import com.tttn.docbao.until.Core;
import com.tttn.docbao.until.News;

@SuppressLint("NewApi")
public class TabTTFragment extends Fragment implements OnItemClickListener {
	private ProgressBar pro;
	private ListView lvtitle;
	private String type = "";
	private ArrayList<News> data;

	public TabTTFragment(String type) {
		this.type = type;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment1, null);
		pro = (ProgressBar) v.findViewById(R.id.progressBar1);
		lvtitle = (ListView) v.findViewById(R.id.lvtitle);
		lvtitle.setOnItemClickListener(this);
		new TrangChuAsyntask().execute();
		return v;
	}

	private class TrangChuAsyntask extends AsyncTask<Object, Object, Object> {
		private ArrayList<NameValuePair> value;
		private int check;;
		private JSONObject jo1, jo2;
		private JSONArray jarr;

		@Override
		protected void onPreExecute() {
			data = new ArrayList<News>();
			value = new ArrayList<NameValuePair>();
			check = 0;
			jo1 = jo2 = null;
			jarr = null;
			super.onPreExecute();
		}

		@Override
		protected Object doInBackground(Object... params) {
			value.add(new BasicNameValuePair("tag", Core.TAG_GETDATA));
			value.add(new BasicNameValuePair("news", Core.convertName(type)));
			value.add(new BasicNameValuePair("action", "thoisu"));
			JSONParser parser = new JSONParser();
			jo1 = parser.getJSONFromUrl(Core.URL, value);

			try {
				check = jo1.getInt("success");
				jarr = jo1.getJSONArray("data");
				for (int i = 0; i < jarr.length(); i++) {
					jo2 = jarr.getJSONObject(i);
					int id = jo2.getInt(Core.KEY_ID);
					int idc = jo2.getInt(Core.KEY_IDDANHMUC);
					String title = jo2.getString(Core.KEY_TIEUDE);
					String content = jo2.getString(Core.KEY_NOIDUNG);
					String date = jo2.getString(Core.KEY_THOIGIAN);
					String des = jo2.getString(Core.KEY_TOMTAT);
					String name = jo2.getString(Core.KEY_TENBAO);
					News news = new News(id, idc, name, title, des, content,
							date);
					data.add(news);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return data;
		}

		@Override
		protected void onPostExecute(Object result) {
			if (result != null) {
				pro.setVisibility(View.GONE);
				lvtitle.setVisibility(View.VISIBLE);
				TitleAdapter adapter = new TitleAdapter(getActivity()
						.getApplicationContext(), (ArrayList<News>) result);
				lvtitle.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "Lỗi mạng xin thử lại",
						Toast.LENGTH_SHORT).show();
			}
			super.onPostExecute(result);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getActivity(), NewsActivity.class);

		intent.putExtra("link", data.get(position).getContent());
		startActivity(intent);
	}

}
